/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { NestedPostFrameCallbackCheck } from '../../../src/checker/performance/NestedPostFrameCallbackCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/NestedPostFrameCallback', rule, CHECK_MODE.PROJECT2CHECK, NestedPostFrameCallbackCheck);
    realPath = checkEntry.scene.getRealProjectDir();
})

describe('NestedPostFrameCallbackCheckTest', () => {
    test('NestedPostFrameCallbackCheckTest_001_Report', () => {
        const detectFile: string = path.join(realPath, 'ets', 'NestedPostFrameCallbackReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 校验所有检测到的告警
        const expected = [
            { line: 17, column: 21, fixKey: '17%21%37%' }
        ];
        expect(detectFileReports.length).toBe(1);
        detectFileReports.forEach((report, idx) => {
            expect(report.defect.reportLine).toBe(expected[idx].line);
            expect(report.defect.reportColumn).toBe(expected[idx].column);
            expect(report.defect.fixKey).toBe(expected[idx].fixKey);
        });
    });

    test('NestedPostFrameCallbackCheckTest_002_NoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'NestedPostFrameCallbackNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });

})
